package cn.source.system.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.source.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.source.system.mapper.ScheduleMapper;
import cn.source.system.domain.Schedule;
import cn.source.system.service.IScheduleService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 课表管理Service业务层处理
 *
 * @author gygl
 * @date 2023-09-06
 */
@Service
public class ScheduleServiceImpl implements IScheduleService
{
    @Resource
    private ScheduleMapper scheduleMapper;

    /**
     * 查询课表管理
     *
     * @param id 课表管理主键
     * @return 课表管理
     */
    @Override
    public Schedule selectScheduleById(Long id)
    {
        return scheduleMapper.selectScheduleById(id);
    }

    /**
     * 查询课表管理列表
     *
     * @param schedule 课表管理
     * @return 课表管理
     */
    @Override
    public List<Schedule> selectScheduleList(Schedule schedule)
    {
        return scheduleMapper.selectScheduleList(schedule);
    }

    /**
     * 新增课表管理
     *
     * @param schedule 课表管理
     * @return 结果
     */
    @Override
    public int insertSchedule(Schedule schedule)
    {
        return scheduleMapper.insertSchedule(schedule);
    }

    /**
     * 修改课表管理
     *
     * @param schedule 课表管理
     * @return 结果
     */
    @Override
    public int updateSchedule(Schedule schedule)
    {
        return scheduleMapper.updateSchedule(schedule);
    }

    /**
     * 批量删除课表管理
     *
     * @param ids 需要删除的课表管理主键
     * @return 结果
     */
    @Override
    public int deleteScheduleByIds(Long[] ids)
    {
        return scheduleMapper.deleteScheduleByIds(ids);
    }

    /**
     * 删除课表管理信息
     *
     * @param id 课表管理主键
     * @return 结果
     */
    @Override
    public int deleteScheduleById(Long id)
    {
        return scheduleMapper.deleteScheduleById(id);
    }

    /**
     * 将Excel表导入数据库
     * @param file
     */
    @Override
    public void importExcel(MultipartFile file) throws IOException {
        InputStream inputStream = null;
        inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream, 0); //指定输入流和sheet
        // 读取第二行到最后一行数据
        List<List<Object>> read = reader.read(1, reader.getRowCount());
        List<Schedule> samples = new ArrayList<>();
        for (List<Object> objects : read) {
            Schedule schedule = new Schedule();
            Object academicYear = objects.get(0);//读取某行第1列数据
            Object semester = objects.get(1); //读取某行第2列数据
            Object dayOfWeek = objects.get(2); //读取某行第3列数据
            Object classNo = objects.get(3); //读取某行第4列数据
            Object startWeek = objects.get(4); //读取某行第5列数据
            Object courseId = objects.get(5); //读取某行第6列数据
            Object courseName = objects.get(6); //读取某行第7列数据
            Object teacherId = objects.get(7); //读取某行第8列数据
            Object teacherName = objects.get(8); //读取某行第9列数据
            Object gender = objects.get(9); //读取某行第10列数据
            Object professionalTitle = objects.get(10); //读取某行第11列数据
            Object educationBackground = objects.get(11); //读取某行第12列数据
            Object department = objects.get(12); //读取某行第13列数据
            Object locationId = objects.get(13); //读取某行第14列数据
            Object locationName = objects.get(14); //读取某行第15列数据
            Object locationType = objects.get(15); //读取某行第16列数据
            Object locationStartWeek = objects.get(16); //读取某行第17列数据
            Object locationClassTime = objects.get(17); //读取某行第18列数据
            Object campus = objects.get(18); //读取某行第19列数据
            Object classSize = objects.get(19); //读取某行第20列数据
            Object classComposition = objects.get(20); //读取某行第21列数据
            Object selectCourseId = objects.get(21); //读取某行第22列数据
            Object credits = objects.get(22); //读取某行第23列数据
            Object totalClassHours = objects.get(23); //读取某行第24列数据
            Object departmentOffering = objects.get(24); //读取某行第25列数据
            Object enrollment = objects.get(25); //读取某行第26列数据
            Object classHoursPerWeek = objects.get(26); //读取某行第27列数据
            Object classTime = objects.get(27); //读取某行第28列数据
            Object location = objects.get(28); //读取某行第29列数据
            Object courseNature = objects.get(29); //读取某行第30列数据
            Object seats = objects.get(30); //读取某行第31列数据
            Object building = objects.get(31); //读取某行第32列数据
            Object phone = objects.get(32); //读取某行第33列数据
            Object major = objects.get(33); //读取某行第34列数据

            schedule.setAcademicYear(academicYear.toString());
            schedule.setSemester(semester.toString());
            schedule.setDayOfWeek(dayOfWeek.toString());
            schedule.setClassNo(classNo.toString());
            schedule.setStartWeek(startWeek.toString());
            schedule.setCourseId(courseId.toString());
            schedule.setCourseName(courseName.toString());
            schedule.setTeacherId(teacherId.toString());
            schedule.setTeacherName(teacherName.toString());
            schedule.setGender(gender.toString());
            schedule.setProfessionalTitle(professionalTitle.toString());
            schedule.setEducationBackground(educationBackground.toString());
            schedule.setDepartment(department.toString());
            schedule.setLocationId(locationId.toString());
            schedule.setLocationName(locationName.toString());
            schedule.setLocationType(locationType.toString());
            schedule.setLocationStartWeek(locationStartWeek.toString());
            schedule.setLocationClassTime(locationClassTime.toString());
            schedule.setCampus(campus.toString());
            schedule.setClassSize((String) classSize);
            schedule.setClassComposition(classComposition.toString());
            schedule.setSelectCourseId(selectCourseId.toString());
            schedule.setCredits((String) credits);
            schedule.setTotalClassHours((String) totalClassHours);
            schedule.setDepartmentOffering(departmentOffering.toString());
            schedule.setEnrollment((String) enrollment);
            schedule.setClassHoursPerWeek((String) classHoursPerWeek);
            schedule.setClassTime((String) classTime);
            schedule.setLocation(location.toString());
            schedule.setCourseNature((String) courseNature);
            schedule.setSeats((String) seats);
            schedule.setBuilding(building.toString());
            schedule.setPhone(phone.toString());
            schedule.setMajor(major.toString());
            //schedule.setCreateTime(DateUtils.getNowDate());

            samples.add(schedule);  //将一行数据存入list中
            Map maps=new HashMap<>();
            maps.put("samples",samples);
            scheduleMapper.insertScheduleList(maps);  //将list存入数据库中
        }
    }

    @Override
    public String judgeClass() {


        return null;
    }


}
